import { ref } from 'vue'

export default function (currentMusic, lyrNode) {

    let lyrIndex = ref(0) //  歌词索引
    let lockLyr = false
    let lockTime = null
    function touchstart() {
        lockLyr = true
        clearTimeout(lockTime) // 再上次定时器还未失效前  把他清掉 重新计时

        lockTime = setTimeout(() => {
            lockLyr = false
        }, 3000)
    }

    function scrollLyr() {
        if (lockLyr) return //  如果是锁定状态(手触摸了 ）  就不让滚动了
        lyrNode.value.scrollTo({
            top: lyrIndex.value < 5 ? 0 : +lyrIndex.value * 24 - 24 * 5,
            behavior: 'smooth'
        })
    }

    function skip(t) {
        if (!currentMusic.value.Lyric) return
        let next = currentMusic.value.Lyric[lyrIndex.value + 1]
        let prev = currentMusic.value.Lyric[lyrIndex.value - 1]
        if (next && t >= next.time) {
            lyrIndex.value++
            skip(t)
        }
        if (prev && t <= prev.time) {
            lyrIndex.value--
            skip(t)
        }
    }

    return {
        touchstart,
        skip,
        scrollLyr,
        lyrIndex
    }
}